<html>
<head><meta charset="utf-8"><title>Optimizing away bounds checks · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html">Optimizing away bounds checks</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="196842021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196842021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196842021">(May 07 2020 at 23:15)</a>:</h4>
<p>I'm trying to figure out under what circumstances rust can optimize away bounds checks if it knows an index is in bounds. I wrote a very simple test case, and can't seem to get rust to omit the bounds checks, even if I use <code>assert!</code>. (I know that I could trivially write it using iterators instead, but I'm trying to figure out rust's ability to optimize here.)</p>



<a name="196842037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196842037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196842037">(May 07 2020 at 23:15)</a>:</h4>
<p>I wrote this:</p>
<div class="codehilite"><pre><span></span><code><span class="cp">#[no_mangle]</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">f</span><span class="p">(</span><span class="n">slice</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="kt">u64</span><span class="p">],</span><span class="w"> </span><span class="n">start</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="n">end</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u64</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">total</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">assert</span><span class="o">!</span><span class="p">(</span><span class="n">start</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">end</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="n">start</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">slice</span><span class="p">.</span><span class="n">len</span><span class="p">());</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">start</span><span class="o">..</span><span class="n">end</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">total</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="n">slice</span><span class="p">[</span><span class="n">i</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">total</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="196842141"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196842141" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196842141">(May 07 2020 at 23:16)</a>:</h4>
<p>I put that into the compiler explorer, with <code>-O</code>, and the resulting assembly looks like this:</p>
<div class="codehilite"><pre><span></span><code>f:
        push    rax
        cmp     rsi, rdx
        jbe     .LBB5_6
        cmp     rdx, rcx
        jae     .LBB5_6
        xor     eax, eax
.LBB5_3:
        cmp     rdx, rsi
        jae     .LBB5_7
        add     rax, qword ptr [rdi + 8*rdx]
        add     rdx, 1
        cmp     rcx, rdx
        jne     .LBB5_3
        pop     rcx
        ret
.LBB5_7:
        lea     rax, [rip + .L__unnamed_5]
        mov     rdi, rdx
        mov     rdx, rax
        call    qword ptr [rip + core::panicking::panic_bounds_check@GOTPCREL]
        ud2
.LBB5_6:
        call    std::panicking::begin_panic
        ud2
</code></pre></div>



<a name="196842249"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196842249" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196842249">(May 07 2020 at 23:18)</a>:</h4>
<p>Based on the x86 calling convention, rdi contains the slice base address, rsi contains the slice length, rdx contains <code>start</code>, and rcx contains <code>end</code>.</p>



<a name="196842284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196842284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196842284">(May 07 2020 at 23:18)</a>:</h4>
<p>So, the first two comparisons verify the assertion and jump to <code>.LBB5_6</code> if it fails, to panic.</p>



<a name="196842324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196842324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196842324">(May 07 2020 at 23:19)</a>:</h4>
<p>Then inside the loop, there's still a comparison of rdx to rsi, and a jump to <code>.LBB5_7</code> to panic if out of bounds.</p>



<a name="196842345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196842345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196842345">(May 07 2020 at 23:19)</a>:</h4>
<p>That's <em>exactly the same comparison</em>.</p>



<a name="196842417"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196842417" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196842417">(May 07 2020 at 23:20)</a>:</h4>
<p>Should rustc be able to optimize away that bounds check?</p>



<a name="196844575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196844575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196844575">(May 07 2020 at 23:48)</a>:</h4>
<p>Submitted as <a href="https://github.com/rust-lang/rust/issues/71997" title="https://github.com/rust-lang/rust/issues/71997">https://github.com/rust-lang/rust/issues/71997</a> .</p>



<a name="196890838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196890838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196890838">(May 08 2020 at 12:47)</a>:</h4>
<p>it would be good, <span class="user-mention" data-user-id="239881">@Josh Triplett</span>, to create comparable C code and compare with clang</p>



<a name="196926603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196926603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196926603">(May 08 2020 at 17:38)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> What would be the equivalent of a bounds-checked slice in C?</p>



<a name="196927732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196927732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196927732">(May 08 2020 at 17:47)</a>:</h4>
<p>manually checking for <code>idx &lt; len</code> I guess <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="196929243"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196929243" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196929243">(May 08 2020 at 17:59)</a>:</h4>
<p>If you're fine with C++, you could use std::vector and its at(i) method, I guess?</p>



<a name="196931755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196931755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196931755">(May 08 2020 at 18:19)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@Hanna Kruppe</span> That works. I'm not even slightly experienced with <em>modern</em> C++, but I know how to use STL.</p>



<a name="196931867"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196931867" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196931867">(May 08 2020 at 18:20)</a>:</h4>
<p>I meant more like, if C++ is as good as C as point of comparison :p</p>



<a name="196931973"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196931973" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196931973">(May 08 2020 at 18:21)</a>:</h4>
<p>Ah, fair.</p>



<a name="196931981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196931981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196931981">(May 08 2020 at 18:21)</a>:</h4>
<p>Worth trying regardless.</p>



<a name="196933424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196933424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196933424">(May 08 2020 at 18:34)</a>:</h4>
<p>Heh, a fair question, I think C++ with <code>at</code> would be fair</p>



<a name="196933433"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196933433" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196933433">(May 08 2020 at 18:34)</a>:</h4>
<p>I'm not so interested in comparing against <em>languages</em></p>



<a name="196933449"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196933449" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196933449">(May 08 2020 at 18:34)</a>:</h4>
<p>as I am in checking whether clang/LLVM does this sort of optimization in ideal cases</p>



<a name="196933461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196933461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196933461">(May 08 2020 at 18:34)</a>:</h4>
<p>and trying to see what aspect of Rust's translation is preventing it from doing so, if so</p>



<a name="196933478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196933478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196933478">(May 08 2020 at 18:35)</a>:</h4>
<p>in other words, <em>rustc</em> wouldn't be doing this optimization, llvm would</p>



<a name="196933695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196933695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196933695">(May 08 2020 at 18:36)</a>:</h4>
<p>but... but... what if wg-mir-opt does it and then noone notices? :P</p>



<a name="196934102"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196934102" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196934102">(May 08 2020 at 18:40)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> I didn't mean that to imply rustc <em>couldn't</em> :)</p>



<a name="196934109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196934109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196934109">(May 08 2020 at 18:40)</a>:</h4>
<p>or <em>shouldn't</em></p>



<a name="196934310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196934310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196934310">(May 08 2020 at 18:42)</a>:</h4>
<p>Language comparisons aside, it turns out that Rust will do this optimization in some cases, but there's what appears to be an off-by-one issue preventing it from doing so in this case.</p>



<a name="196934333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196934333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196934333">(May 08 2020 at 18:42)</a>:</h4>
<p>See <a href="https://github.com/rust-lang/rust/issues/71997#issuecomment-625958662" title="https://github.com/rust-lang/rust/issues/71997#issuecomment-625958662">https://github.com/rust-lang/rust/issues/71997#issuecomment-625958662</a> and the comment above it.</p>



<a name="196934399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196934399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196934399">(May 08 2020 at 18:43)</a>:</h4>
<p>(That's leaving aside that the <code>assert!</code> should be enough, without the <code>min</code>.)</p>



<a name="196936665"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196936665" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196936665">(May 08 2020 at 19:01)</a>:</h4>
<p>I just confirmed that naively translated test cases in C++ have about the same effect.</p>



<a name="196936818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196936818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196936818">(May 08 2020 at 19:02)</a>:</h4>
<p>clang with C++ can't eliminate the bounds check for <code>.at(i)</code> with just the assert, nor can it with <code>if (end &gt; slice.size()) end = slice.size();</code>, but it <em>can</em> if I change that to <code>&gt;=</code>.</p>



<a name="196936851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/196936851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#196936851">(May 08 2020 at 19:02)</a>:</h4>
<p>And the generated code looks approximately the same, modulo how C++ spells the "panic on bounds check failure".</p>



<a name="197006332"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/197006332" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nikita Popov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#197006332">(May 09 2020 at 18:50)</a>:</h4>
<p>I poked at the C++ case a bit. The bounds check gets hoisted out of the loop (this is a new "loop exit predication" feature in LLVM 10), but not optimized away. (It would be possible to get a pretty good result by performing a loop unswitch after induction variable simplification, which would keep only a single unnecessary check before the loop.)</p>



<a name="197006395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/197006395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nikita Popov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#197006395">(May 09 2020 at 18:52)</a>:</h4>
<p>Neither SCEV nor InstCombine manage to fold the hoisted check. On the SCEV level we need to prove that <code>(((-1 * %start) + (%slice_len umax %start)) umin (-1 + (-1 * %start) + %end)) &lt; ((-1 * %start) + (%slice_len umax %start))</code>, which is not so easy. The <code>%slice_len umax %start</code> parts really should get folded, but don't, because only non-recursive reasoning is used for that, while loop-entry-guard reasoning is needed to simplify.</p>



<a name="197006465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/197006465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nikita Popov <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#197006465">(May 09 2020 at 18:55)</a>:</h4>
<p>For the rest, things would go well if we could eliminate the (-1 * %start) part from both sides, but that's only possible under nowrap conditions. Which we do have here, but that requires reasoning about multiple conditions at the same time.</p>



<a name="197011747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Optimizing%20away%20bounds%20checks/near/197011747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Optimizing.20away.20bounds.20checks.html#197011747">(May 09 2020 at 20:38)</a>:</h4>
<p>When I tested with godbolt and LLVM trunk, it didn't pull the bounds check out of the lop.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>